﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ToggleItemBtn : MonoBehaviour
{
    Toggle toggle;
    Text TopicTytleText;
    TopicPanel topic;
    GameObject Dui;
    //GameObject Cuo;
    private void Awake()
    {
        TopicTytleText = ToolUnity.FindGameObject(gameObject, "Label").GetComponent<Text>();
        Dui = ToolUnity.FindGameObject(gameObject, "对号");
        //Cuo = ToolUnity.FindGameObject(gameObject, "叉叉");
        toggle = GetComponent<Toggle>();
    }

    public void SetData(TopicPanel topicPanel,string Msg,bool b = false)
    {
        topic = topicPanel;
        TopicTytleText.text = Msg;
        toggle.isOn = b;
        if (topic.tempTopic.Interactable == false)
        {
            ShowImage(IsRealAnswer(Msg, topic.tempTopic));
        }
    }

    public bool GetToggle()
    {
        return toggle.isOn;
    }

    public string GetTytle()
    {
        return TopicTytleText.text;
    }

    /// <summary>
    /// 判断是否是正确答案
    /// </summary>
    /// <param name="Msg"></param>
    /// <returns></returns>
    private bool IsRealAnswer(string Msg,TopicItemDM topicItemDM)
    {
        for (int j = 0; j < topicItemDM.RealAnswerList.Count; j++)
        {
            if (Msg == topic.tempTopic.RealAnswerList[j])
            {
                return true;
            }
        }
        return false;
    }


    public void ShowImage(bool b)
    {
        if (b)
        {
            Dui.SetActive(true);
            //Cuo.SetActive(false);
        }
        else
        {
            Dui.SetActive(false);
            //Cuo.SetActive(true);
        }
    }

    void Start()
    {
        toggle.onValueChanged.AddListener(e=> 
        {
            if (e)
            {
                if (topic!=null)
                {
                    TopicSystem.Instance.GetTopicItemDMbyTytle(topic.GetTytleText()).PlayAnswerList.Add(TopicTytleText.text);
                    Debug.Log("增加：" + TopicTytleText.text);
                }
            }
            else
            {
                if (topic != null)
                {
                    TopicSystem.Instance.GetTopicItemDMbyTytle(topic.GetTytleText()).PlayAnswerList.Remove(TopicTytleText.text);
                    Debug.Log("移除：" + TopicTytleText.text);
                }
            }
        });
    }


}
